package com.cpecc.controller.wuzi;

import com.cpecc.framework.web.APIResult;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.wuzi.*;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.BaoFeiSPR;
import com.cpecc.model.wuzi.WuZi;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/wuzi/")
public class BaoFeiDanController {

	@Autowired
	private BaoFeiManager baoFeiManager;

	@Autowired
	private UserManager userManager;

	@Autowired
	private BaoFeiSPRManager baoFeiSPRManager;

	@Autowired
	private BaoFeiJLManager baoFeiJLManager;

	@Autowired
	private WuZiManager wuZiManager;

	@Autowired
	private JiYaoManager jiYaoManager;

	@Autowired
	private FuJianManager fuJianManager;

	/**
	 * 查询报废待办列表
	 * @return
	 */
	@RequestMapping(value = "/baoFeiDaiBan")
	public ModelAndView baoFeiDaiBan(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();

		// 报废相关部门审批
		List<BaoFeiSPR> bfxgbmspList = baoFeiSPRManager.getBfxgbmspList(user);
		mav.addObject("bfxgbmspList", bfxgbmspList);

		if (currentUser.hasRole("低值管理员")) {
			mav.addObject("yiShenQingBaoFeiList", baoFeiManager.getBaoFeiList(703));
		}

		if(currentUser.hasRole("部门负责人")){
			List<BaoFeiSPR> baoFeiBMList = baoFeiSPRManager.getMyBMSPList(user);
			mav.addObject("baoFeiBMList", baoFeiBMList);
		}

		mav.setViewName("baofei/baoFeiDaiBan");
		return mav;
	}

	/**
	 * 获取报废待办数量
	 * @param response
	 */
	@RequestMapping(value = "/getBaoFeiShu")
	public void getBaoFeiShu(HttpServletResponse response){
		int zongShu=0;

		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		zongShu += baoFeiSPRManager.getBfxgbmspList(user).size();
		if (currentUser.hasRole("低值管理员")) {
			zongShu += baoFeiManager.getBaoFeiList(703).size();
		}

		if(currentUser.hasRole("部门负责人")){
			zongShu += baoFeiSPRManager.getMyBMSPList(user).size();
		}

		response.setContentType("application/json;charset=utf-8");
		String result="{\"result\":\"("+zongShu+")\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/gzglyspList")
	public ModelAndView gzglyspList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiShenQingBaoFeiList", baoFeiManager.getBaoFeiList(703));
		mav.setViewName("baofei/gzglyspListAjax");
		return mav;
	}

	@RequestMapping(value = "/bmspList")
	public ModelAndView bmspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("baoFeiBMList", baoFeiSPRManager.getMyBMSPList(user));
		mav.setViewName("baofei/bmspListAjax");
		return mav;
	}

	/**
	 * 归口部门审批列表
	 * @return
	 */
	@RequestMapping(value = "/baofeidan/bfxgbmspList")
	public ModelAndView bfxgbmspList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("bfxgbmspList", baoFeiSPRManager.getBfxgbmspList(user));
		mav.setViewName("baofei/baofeidan/bfxgbmspListAjax");
		return mav;
	}

	@RequestMapping(value = "/bfhqBHPL")
	public ModelAndView bfhqBHPL(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("baoFeiId");
		String nextHuanJie = request.getParameter("nextHuanJie");
		String container = request.getParameter("container");
		String url = request.getParameter("url");
		if(type!=null){
			String bfhqIds="";
			for(int i=0;i<type.length;i++){
				bfhqIds=bfhqIds+type[i]+",";
			}
			if(bfhqIds.length()>0)
				bfhqIds=bfhqIds.substring(0, bfhqIds.length()-1);

			mav.addObject("bfhqIds",bfhqIds);
			mav.addObject("nextHuanJie", nextHuanJie);
			mav.addObject("container", container);
			mav.addObject("url", url);
		}
		mav.setViewName("baofei/huiQianPLBTY");
		return mav;
	}

	// TODO 添加部门领导审批 702
	/**
	 * 提交申请
	 * @param request
	 */
	@RequestMapping(value = "/baoFeiSQ", method = RequestMethod.POST)
	@ResponseBody
	public APIResult baoFeiSQ(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		String wuZiId=request.getParameter("wuZiId");
		String baoFeiYY=request.getParameter("baoFeiYY");
		int baoFeiLX=Integer.parseInt(request.getParameter("baoFeiLX"));

		WuZi wuZi=wuZiManager.get(wuZiId);
		BaoFei baoFei=new BaoFei();
		baoFei.setBaoFeiLX(baoFeiLX);
		baoFei.setBaoFeiYY(baoFeiYY);
		baoFei.setShenQingRQ(new Date());
		baoFei.setUser(user);
		baoFei.setWuZi(wuZi);
		baoFei.setDangQianS(1);
		baoFei.setZongRS(0);
		baoFeiManager.submit(baoFei, user);
		return APIResult.success();
	}

	/**
	 * 报废审批
	 */
	@RequestMapping(value = "/baofei/check", method = RequestMethod.POST)
	@ResponseBody
	public APIResult check(
			@RequestParam("baoFeiId") String baoFeiId,
			@RequestParam("nextHuanJie") int nextHuanJie,
			@RequestParam(name = "content", required = false, defaultValue = "同意") String content) {
		String[] arryBaoFeiId = baoFeiId.split(",");
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		baoFeiManager.check(arryBaoFeiId, nextHuanJie, content, user);
		return APIResult.success();
	}

	@RequestMapping(value = "/baoFeiDetail")
	public ModelAndView baoFeiDetail(String id){
		ModelAndView mav=new ModelAndView();
		BaoFei baoFei=baoFeiManager.get(id);
		mav.addObject("baoFei", baoFei);
		mav.addObject("jiYaoList", jiYaoManager.list(baoFei));
		mav.addObject("fuJianList", fuJianManager.list(baoFei));
		mav.addObject("xiaYiBuR", getXiaYiBuR(id));
		mav.addObject("baoFeiJLList", baoFeiJLManager.getAllJL(id));
		mav.setViewName("baofei/baoFeiDetail");
		return mav;
	}

	private List<User> getXiaYiBuR(String baoFeiId){
		List<User> list=new ArrayList<User>();
		BaoFei baoFei=baoFeiManager.get(baoFeiId);
		int huanJie=baoFei.getHuanJie().getId();
		switch(huanJie){
			// 报废部门审批、不同意
			case 702:
				list.addAll(baoFeiSPRManager.getBuMenSPR(baoFeiId)); break;

			//管理员审批、不同意
			case 703:
				list.addAll(userManager.getGZGLY()); break;

			// 归口部门负责人审批、不同意
			case 717:
				list.addAll(baoFeiSPRManager.getSPRList(baoFeiId)); break;
			default: break;
		}
		return list;
	}



}
